# -*- coding: utf-8 -*-
# @Time    : 2016/9/23
import re
from mathsolver.functions.base import BaseNumber, new_latex, BaseFunction
from mathsolver.functions.daoshu.daoshuyunsuan import HanShuDaoShuYunSuan
from mathsolver.functions.daoshu.dingyishibianhuan import DingYiShiBianHuanJiSuan


class HanShuDaoShuJiSuan(BaseFunction):
    """利用导数的定义求函数在某处的导数"""

    def solver(self, *args):
        fun, lim = args
        bianhuan_obj = DingYiShiBianHuanJiSuan().solver(lim)
        bianhuan_res = bianhuan_obj.output[0].sympify()

        match = re.search(r"f_d\((.*?)\)", str(bianhuan_res))
        a = match.group(1)

        # 对 f(x) 求导，把 a 带到求导结果里面
        result = fun.expression.diff(fun.var).subs(fun.var, a)
        daoshu_obj = HanShuDaoShuYunSuan().solver(fun)
        bianhuan_obj.steps.extend(daoshu_obj.steps)
        bianhuan_obj.steps.append(
            ["将 {} = {} 代入 {}'({})，结果为：{}".format(fun.var, a, fun.name, fun.var, new_latex(result)), ])

        bianhuan_obj.output = [BaseNumber(result)]
        bianhuan_obj.label = {"利用导数的定义求函数在某处的导数", }

        return bianhuan_obj


if __name__ == "__main__":
    pass
